package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Resource
    private SetmealDishService setmealDishService;

    @Resource
    private DishService dishService;

    //新增套餐，同时需要保存套餐和菜品的关联关系
    @Override
    public void savaWhitDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作stemeal,执行insert操作
        this.save(setmealDto);

        //获取套餐和菜品关联对象
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存套餐和菜品的关联信息，操作stemeal_dish执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    //删除套餐，同时需要删除套餐和菜品的关联数据
    @Override
    @Transactional
    public void removeWhitDish(List<Long> ids) {
        //查询套餐状态，确定是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加多个条件,如: where id in (1,2,3)
        queryWrapper.in(Setmeal::getId,ids);
        //判断是否相等
        queryWrapper.eq(Setmeal::getStatus,1);

        //获取起售状态的套餐
        int count = this.count(queryWrapper);
        if (count > 0){
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        //如果可以删除，先删除套餐中的信息。setmeal
        this.removeByIds(ids);

        LambdaQueryWrapper<SetmealDish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加多个条件,如: where id in (1,2,3)
        dishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);

        //删除套餐和菜品关联表中的数据
        setmealDishService.remove(dishLambdaQueryWrapper);
    }

    //根据id获取套餐和菜品数据
    @Override
    public SetmealDto getSetmealDtoData(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(id != null,SetmealDish::getSetmealId,id);

        if (setmeal != null){
            //进行拷贝
            BeanUtils.copyProperties(setmeal,setmealDto);
            //根据id查询套餐和菜品对象
            List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);
            setmealDto.setSetmealDishes(setmealDishList);
            return setmealDto;
        }
        return null;
    }

}
